package service.book;

import java.sql.Connection;
import java.util.ArrayList;

import dao.book.BorrowDao;
import dao.book.BorrowDaoImpl;
import entity.Book;
import entity.Borrow;
import entity.User;
import service.user.UserService;
import service.user.UserServiceImpl;
import utils.SqlConnection;

/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 
 *
 */
public class BorrowServerImpl implements BorrowServer {
	Connection connection;
	BorrowDao borrowDao = new BorrowDaoImpl();
	BookServer bookServer = new BookServerImpl();
	UserService userService = new UserServiceImpl();
	
	ArrayList<Book> bookList = new ArrayList<Book>();
	ArrayList<User> userList = new ArrayList<User>();
	@Override
	public Borrow getBorrowInfo(Borrow borrow) {
		// TODO Auto-generated method stub
		
		connection = SqlConnection.getConnection();
		int count = 0;
		Borrow borrow1 = new Borrow();
		try {
//			count = borrowDao.getBorrowCount(connection, borrow);
			
			
		 	ArrayList<Borrow> borrowList =  (ArrayList<Borrow>) borrowDao.getBorrwList(connection, borrow);
		 	count = borrowList.size();
		 	System.out.println("查询已借----"+borrowList.size());
		 	if(count>0) {
		 		borrow1 = borrowList.get(0);
		 	}
		   
			
			bookList = (ArrayList<Book>) bookServer.getBookList(borrow.getBook());
			userList = (ArrayList<User>) userService.getUserList(borrow.getUser());
			
			Book book = bookList.size()>0?bookList.get(0):null;
			User user = userList.size()>0?userList.get(0):null;
			
			borrow1.setBook(book);
			borrow1.setUser(user);
			borrow1.setCount(count);
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		
		return borrow1;
	}
	@Override
	public boolean addBorrow(Borrow borrow) {
		// TODO Auto-generated method stub
		connection = SqlConnection.getConnection();
		
		int row = 0;
		
		try {
			row = borrowDao.addBorrow(connection, borrow);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return row>0;
	}
	@Override
	public boolean upBorrow(Borrow borrow) {
		// TODO Auto-generated method stub
		if (borrow == null)
			borrow = new Borrow();
		connection = SqlConnection.getConnection();
		int row = 0;
		try {
			row = borrowDao.upBorrow(connection, borrow);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return row > 0;
	}

}
